package com.wxq.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.wxq.product.entity.AttrEntity;
import com.wxq.product.service.*;
import com.wxq.product.vo.AttrGroupRelationV0;
import com.wxq.product.vo.AttrGroupWithAttrsV0;
import com.wxq.product.entity.AttrEntity;
import com.wxq.product.entity.AttrGroupEntity;
import com.wxq.product.service.AttrAttrgroupRelationService;
import com.wxq.product.service.AttrGroupService;
import com.wxq.product.service.AttrService;
import com.wxq.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wxq.common.utils.PageUtils;
import com.wxq.common.utils.R;



/**
 * 属性分组
 *
 * @author jim
 * @email 2982877319@qq.com
 * @date 2022-03-13 19:57:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;


    /**
     * 查询属性分组未关联的属性
     * @param attrgroupId
     * @param params
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoAttrRelation(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     * 获取分类下所有分组及关联的所有属性
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
     public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        List<AttrGroupWithAttrsV0> v0s = attrGroupService.getAttrGroupWhthAttrsByCatelogId(catelogId);
        return R.ok().put("data",v0s);
     }
    /**
     * 查询属性分组与属性关联关系
     * @param attrgroupId
     * @return 当前属性分组的属性集合
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntities = attrService.getAttrRelation(attrgroupId);
        return R.ok().put("data",attrEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 查找三级分类的完整路径 [level1Id, level2Id, level3Id]
        Long[] catelogPath = categoryService.findCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 新增属性与分组关联关系
     * @param v0s
     * @return
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationV0> v0s){
        relationService.addRelation(v0s);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除关联关系
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationV0[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

}
